package com.lvdp.utils;

import com.lvdp.dto.UserDTO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description: 登录拦截器
 * Date:      2022/11/1 21:38
 * author:    LPH
 * version:   V1.0
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断是否要拦截
        UserDTO user = UserHolder.getUser();
        if (user.getId() == null) {
            response.setStatus(401);
            return false;
        }
        // 有用户 就放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 移除用户
        UserHolder.removeUser();
    }
}
